<template>
  <el-dialog :title="isEdit ? '编辑管理员' : '新增管理员'" v-model="visible" width="400px" @close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="90px" status-icon>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" :disabled="isEdit" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!isEdit">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择角色">
          <el-option label="超级管理员" value="1" />
          <el-option label="商品管理员" value="2" />
          <el-option label="数据分析员" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-radio-group v-model="form.enable">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">{{ isEdit ? '保存' : '添加' }}</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  visible: Boolean,
  isEdit: Boolean,
  adminId: [Number, String],
  modelValue: Boolean,
  adminData: Object
})
const emit = defineEmits(['update:visible', 'success'])

const visible = ref(props.visible)
watch(() => props.visible, v => visible.value = v)
watch(visible, v => emit('update:visible', v))

const formRef = ref()
const form = reactive({
  username: '',
  email: '',
  password: '',
  role: '',
  enable: 1
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度3-20位', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度6-20位', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ],
  enable: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 回显数据
watch(() => props.adminData, (val) => {
  if (props.isEdit && val) {
    Object.assign(form, val)
    form.password = ''
  } else if (!props.isEdit) {
    form.username = ''
    form.email = ''
    form.password = ''
    form.role = ''
    form.enable = 1
  }
}, { immediate: true })

const handleClose = () => {
  emit('update:visible', false)
}

const handleSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return
    emit('success', { ...form })
  })
}
</script>
